package com.yycx.module.bus.provider.handler.haiying.saleman;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.DeptTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.HyOrderUtils;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanBalanceService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import com.yycx.module.user.client.entity.AppPosition;
import com.yycx.module.user.provider.service.AppPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;

/**
 * 海鹰业务员保存或者更新处理器
 *
 * @author zyf
 */
@Component
public class HyDeptSaleManSaveHandler implements SaveInterceptor<BusSaleman> {
    @Resource
    private BusDeptService deptService;


    @Resource
    private SysCompanyService companyService;


    @Resource
    private AppPositionService positionService;


    @Autowired
    @Lazy
    private BusSalemanService salemanService;


    @Autowired
    @Lazy
    private BusSalemanBalanceService salemanBalanceService;

    @Autowired
    @Lazy
    private HyOrderUtils hyOrderUtils;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusSaleman saleMan) {
        initDeptBusSaleMan(saleMan);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusSaleman saleMan) {

    }


    @Override
    public void prepare(CriteriaUpdate cu, EntityMap entityMap, BusSaleman saleMan) {
        initDeptBusSaleMan(saleMan);
    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BusSaleman saleMan) {
    }


    /**
     * 初始化部门人员
     *
     * @param busSaleman
     */
    private void initDeptBusSaleMan(BusSaleman busSaleman) {
        Long companyId = FlymeUtils.getLong(busSaleman.getCompanyId(), OpenHelper.getCompanyId());
        Long deptId = busSaleman.getDeptId();
        BusDept busDept = deptService.getById(deptId);
        String deptCode = busDept.getDeptCode();
        ApiAssert.isNotEmpty("部门编码未设置", deptCode);
        //部门类型
        String deptType = busDept.getDeptType();
        busSaleman.setDeptType(deptType);
        String positionNo = "";
        //职务ID
        Long positionId = busSaleman.getPositionId();
        if (FlymeUtils.isNotEmpty(positionId)) {
            AppPosition position = positionService.getById(positionId);
            positionNo = position.getPositionNo();
            ApiAssert.isNotEmpty("职务编码未设置", positionNo);
        }
        //检测业务员是否存在
        checkSaleMan(busSaleman, companyId, 2);

        //职务序号
        String positionOrder = getPositionOrder(busSaleman, companyId, deptType);
        //唯一编码
        String saleManCode = busSaleman.getSaleManCode();
        String positionCode = null;
        String headCode = null;
        //自营部门加序号
        if (DeptTypeEnum.ZY.getCode().equals(deptType)) {
            if (!positionNo.contains("F")) {
                //部门人员设置负责人编码
                positionCode = positionNo + positionOrder;
                headCode = deptCode + positionCode;
            }
        }
        Long parentId = busSaleman.getParentId();
        //招募人
        if (FlymeUtils.isNotEmpty(parentId)) {
            //招募人
            BusSaleman parent = salemanService.getById(parentId);
            if (FlymeUtils.isNotEmpty(parent)) {
                busSaleman.setParentName(parent.getSaleManName());
            }
        }
        busSaleman.setSaleManCode(null);
        busSaleman.setSaleManCode(saleManCode);
        SysCompany company = companyService.getById(companyId);
        Long organizationId = company.getOrganizationId();
        busSaleman.setPositionCode(positionCode);
        busSaleman.setHeadCode(headCode);
        busSaleman.setSalemanState(1);
        busSaleman.setCompanyId(companyId);
        busSaleman.setSaleManType(2);
        busSaleman.setSaleManBusinessNo(null);
        busSaleman.setOrganizationId(organizationId);
    }


    /**
     * 检测自营业务员是否存在(按身份证检测)
     *
     * @param busSaleman
     * @return
     */
    private Boolean checkSaleMan(BusSaleman busSaleman, Long companyId, Integer saleManType) {
        //职务序号
        CriteriaQuery cq = new CriteriaQuery(BusSaleman.class);
        cq.eq(FlymeUtils.isNotEmpty(busSaleman.getCardNo()), "cardNo", busSaleman.getCardNo());
        cq.eq(FlymeUtils.isNotEmpty(busSaleman.getSaleManName()), "saleManName", busSaleman.getSaleManName());
        cq.eq(true, "deptId", busSaleman.getDeptId());
        cq.eq(true, "positionId", busSaleman.getPositionId());
        cq.eq(true, "saleManType", saleManType);
        cq.eq(FlymeUtils.isNotEmpty(busSaleman.getPositionOrder()), "positionOrder", busSaleman.getPositionOrder());
        cq.ne(FlymeUtils.isNotEmpty(busSaleman.getSaleManId()), "saleManId", busSaleman.getSaleManId());
        //检测同部门同职务同职务序号的人是否存在
        Long userCount = salemanService.count(cq);
        ApiAssert.lezero("人员已存在,请勿重复添加", userCount.intValue());
        return true;
    }

    private String getPositionOrder(BusSaleman busSaleman, Long companyId, String deptType) {
        //职务序号
        String positionOrder = busSaleman.getPositionOrder();
        if (FlymeUtils.isEmpty(positionOrder)) {
            CriteriaQuery cq1 = new CriteriaQuery(BusSaleman.class);
            cq1.eq(true, "deptId", busSaleman.getDeptId());
            cq1.eq(true, "positionId", busSaleman.getPositionId());
            cq1.eq(true, "companyId", companyId);
            Long count = salemanService.count(cq1);
            Long positionCount = FlymeUtils.getLong(count, 0L);
            if (DeptTypeEnum.ZY.getCode().equals(deptType)) {
                positionOrder = String.format("%02d", positionCount.intValue() + 1);
            } else {
                if (positionCount >= 1) {
                    positionOrder = String.format("%03d", positionCount.intValue() - 1 + 1);
                }
            }
            busSaleman.setPositionOrder(positionOrder);
        }
        return positionOrder;
    }
}
